[Windows/MinGW64] Fixes for TBitArray dllimport, Mesh::DeleteMesh double free, and lazy init #229
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR provides a set of fixes that allow Netgen 6.2.x to build and run
correctly on Windows + MSYS2 MinGW64 (GCC 13.x).
Without these fixes, Netgen cannot be built reliably, and even when it builds,
Ng_DeleteMesh()consistently crashes with heap corruption (0xC0000374) due todouble freeing.
After applying these changes, Netgen compiles cleanly and
libnglib.dllworksstably with OCC geometries for edge, surface, and volume meshing.
All modifications are minimal, isolated, and affect Windows/Mingw only.
Issues Fixed in This PR
1) Heap corruption due to double deletion
Mesh::DeleteMesh()andMesh::~Mesh()both freed:bcnamescd2namescd3namesThis resulted in:
STATUS_HEAP_CORRUPTION (0xC0000374)ntdll!RtlFreeHeapNg_DeleteMesh(mesh)after meshingDeleteMesh()now resets internal state only, and the destructor performs thefinal cleanup, eliminating the double-free.
2) Invalid
dllimporton inline template members (TBitArray)In
bitarray.hpp, inline template definitions were marked withNGCORE_API(__declspec(dllimport)on Windows).MinGW rejects dllimport on inline definitions and then fails at link time:
undefined reference to __imp__TBitArray::Or(...)
This PR removes
NGCORE_APIfrom inline template definitions.Template behavior remains unchanged, but MinGW no longer fails to link.
3) Unsafe lazy initialization
Several global/static objects were dereferenced before safe initialization on
Windows, producing undefined behavior.
These have been replaced with safe local statics or guarded initialization.
4) Missing
<mutex>include on MinGWmeshing/global.hppusesstd::mutexbut did not include<mutex>.<mutex>indirectly via other headers.'mutex' does not name a typeThis PR adds an explicit
#include <mutex>wherestd::mutexis used(inside the Netgen headers), which fixes the build on MinGW without affecting
MSVC.
5)
DLL_HEADERexpands to__declspec(dllimport)on MinGW for definitionsOn MinGW,
DLL_HEADERexpanded to__declspec(dllimport)as on MSVC.This is fine for declarations, but caused errors for inline / locally-defined
functions, for example:
function 'void netgen::DenseMatrix::Mult(...)' definition is marked dllimportfunction 'netgen::Element::Element()' definition is marked dllimport~FaceDescriptor() definition is marked dllimportIn this PR,
DLL_HEADERis redefined for MinGW builds so that these functiondefinitions are no longer marked as
dllimport.This keeps the DLL API consistent while allowing MinGW to compile and link
successfully.
6) CMake fixes for MSYS2 MinGW64
The default CMake configuration included several Linux-only flags and did not
correctly detect MSYS2’s OpenCascade layout.
Fixes include:
USE_SUPERBUILD=OFFworks with MinGWAfter these changes, Netgen builds cleanly with MSYS Makefiles.
Build Configuration Used
Results
After applying this PR:
Netgen builds successfully on MSYS2 MinGW64 (GCC 13.x)
libnglib.dll loads correctly via C/C++ API
OCC → edge/surface/volume mesh generation works correctly
Ng_DeleteMesh() no longer crashes
No API or ABI changes
All fixes are minimal and contained
Minimal Reproduction Code 1 (MinGW)
Minimal Reproduction Code 2 (MinGW)